using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;


public class GameManager : MonoBehaviour
{
    [SerializeField] private GameObject pauseScreen;
    [SerializeField] private GameObject GameOverScreen;
    [SerializeField] private TextMeshProUGUI scoreText;

    private PlayerController player;
    private LivesUIController livesUIController;
    private AudioSource musicSource;
    private int score;
    public int health { get; private set; }
    public bool isPause = false;
    
    private void Start()
    {
        livesUIController = GameObject.FindGameObjectWithTag("Lives").GetComponent<LivesUIController>();
        musicSource = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<AudioSource>();
        player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
        
        health = 3;
        livesUIController.UpdateLives(health);
        
        scoreText.text = "0";
        
        DataManager.Instance.LoadSettings();
        musicSource.volume = DataManager.Instance.MusicSettingValue;
    }

    public void AddLife(int livesToAdd)
    {
        health += livesToAdd;
        if (health > 5)
        {
            health = 5;
        }
        livesUIController.UpdateLives(health);
    }
    
    public void RemoveLife(int livesToRemove)
    {
        health -= livesToRemove;
        livesUIController.UpdateLives(health);
        if (health <= 0)
        {
            player.gameObject.SetActive(false);
            GameOver();
        }
    }

    public void UpdateScore(int updateScore)
    {
        score += updateScore;
        scoreText.text = score.ToString();
    }

    private void GameOver()
    {
        isPause = true;
        GameOverScreen.SetActive(true);
        Time.timeScale = 0;
    }

    public void RestartGame()
    {
        SceneManager.LoadScene(1);
        isPause = false;
        Time.timeScale = 1;
    }

    
    public void PauseGame()
    {
        Time.timeScale = 0;
        isPause = true;
        pauseScreen.SetActive(true);
    }

    public void UnPauseGame()
    {
        Time.timeScale = 1;
        isPause = false;
        pauseScreen.SetActive(false);
    }
}